<template>
  <div class="curtomerBox">
    <div class="top">
      <img
        src="../../assets/men.png"
        width="40px"
        style="margin-left: 4px"
        height="40px"
        alt=""
        @click="backClick"
      />
      <span style="font-size: 22px;font-weight: 900;">在线客服</span>
    </div>
    <div class="twoCheck">
<img src="../../assets//doc.png" alt="" style="width: 414px;height: 80px;">
    </div>
    <div class="message_chat">
      <div class="messageContent">
        <div class="message" v-for="(item, index) in messageList" :key="index">
          <div class="message_left" v-if="item.type === 'server'">
            <div class="avatar">
              <img
                src="../../assets/men.png"
                style="width: 50px; height: 50px; margin: 0px 10px; margin-top: 15px"
                alt=""
              />
            </div>
            <div class="Leftcontents">
              <div class="top">
                <span>客服 {{ item.time }}</span>
              </div>
              <div class="messageCon">
                {{ item.content }}
              </div>
            </div>
          </div>
          <div class="message_right" v-if="item.type === 'person'">
            <div class="Rightcontents">
              <div>
                <span>{{ item.time }} 我的</span>
              </div>
              <div class="RightmessageCon">
                <div v-if="item.content.startsWith('https')">
                  <img :src="item.content" class="RightImage" alt="" />
                </div>
                <div v-else>
                  {{ item.content }}
                </div>
              </div>
            </div>
            <div class="avatar">
              <img
                src="../../assets/imgss.png"
                style="width: 70px; height: 70px; margin: 0px 10px; margin-top: 5px"
                alt=""
              />
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="sendMessageInput">
      <div class="sendTops">
        <img
          src="../../assets/updaded.png"
          @click="handlePic"
          style="width: 30px; height: 30px; margin: 0 10px; margin-left: 20px"
          alt=""
        />
        <div class="sendInput">
          <input type="text" placeholder="请输入" v-model="MessageE" />
          <div :class="showEmoji ? 'emojiImage' : 'InputImage'" @click="handleImage"></div>
        </div>
        <input ref="fileInput" @change="handleFile" type="file" style="display: none" />
        <div>
          <img
            src="../../assets/mess.png"
            @click="handleSend"
            style="margin-right: 10px;
            width: 30px;height: 30px;"
            alt=""
          />
        </div>
      </div>
      <div class="sendContent" v-show="showEmojiDown">
        <div class="emoji">
          <div v-for="(item, ind) in emojis" :key="ind" style="fon" @click="sendEmoji(item)">
            {{ item }}
          </div>
        </div>
      </div>
      <div class="sendFooter"></div>
    </div>
  </div>

  <el-dialog
    v-model="dialogVisible"
    title="Tips"
    width="350"
    height="300"
    :before-close="handleClose"
  >
    <span>发送了一条信息</span>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">Cancel</el-button>
        <el-button type="primary" @click="dialogVisible = false"> Confirm </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<style lang="scss" scoped>
.curtomerBox {
  width: 100%;
  height: 736px;
  display: flex;
  flex-direction: column;
  position: relative;
  overflow: hidden;
}
.top {
  display: flex;
  align-items: center;
  height: auto;
  padding: 2% 0%;
  justify-content: space-between;
  span{
    font-size: 18px;
  }
}
.twoCheck {
  height: auto;
  padding: 1% 0%;
  line-height: 80px;
  // background-color: #2bce79;
  display: flex;
  align-items: center;
  justify-content: space-between;
  img {
    width: 60px;
    height: 60px;
  }
}
.message_chat {
  padding: 2% 0%;
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: auto;
}
.messageContent {
  width: 414px;
  height: 250px; 
  font-size: 20px;
  // flex: 1;
  overflow-y: scroll;
  background-color: #ffffff;
}
.contentMessage {
  margin: 10px 5px;
  background: #f2f2fb;
  padding: 5px 20px;
  width: 92%;
  height: 40px;
  font-size: 20px;
  border-radius: 25px;
  line-height: 40px;
  box-shadow: 0 1px 6px 0 #cfd1d3;
}
.contentMessageRight {
  margin-left: 50px;
  margin-top: 10px;
  background: #f2f2fb;
  padding: 5px 20px;
  font-size: 27px;
  border-radius: 25px;
  line-height: 40px;
  box-shadow: 0 1px 6px 0 #cfd1d3;
}
.message {
  width: 100%;
  overflow: auto;
  margin: 10px 0px;
}
.message_left {
  height: auto;
  width: 90%;
  display: flex;
  justify-content: flex-start;
}
.Leftcontents {
  display: flex;
  flex-direction: column;
  line-height: 40px;
  span {
    font-size: 18px;
  }
}
.Rightcontents {
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  text-align: right;
  line-height: 60px;
  span {
    display: inline-block;
    font-size: 20px;
    margin-right: 10px;
  }
}
.messageCon {
  background-color: #f1f2fb;
  font-size: 20px;
  line-height: 50px;
  font-family: '楷体';
  padding: 5px 20px;
  box-shadow: 0 1px 6px 0 #cfd1d3;
  border-radius: 5px 25px 25px 25px;
}
.RightmessageCon {
  background-color: #f1f2fb;
  font-size: 26px;
  line-height: 50px;
  font-family: '楷体';
  box-shadow: 0 1px 6px 0 #cfd1d3;
  border-radius: 25px 5px 25px 25px;
  padding: 5px 20px;
  text-align: center;
  .RightImage {
    max-height: 170px;
    max-width: 290px;
    margin-top: 10px;
  }
}
.message_right {
  display: flex;
  justify-content: flex-end;
  width: 97%;
  height: auto;
}

.topMessage {
  display: flex;
  align-items: center;
  justify-content: start;
}
.flex-left {
  display: flex;
  align-items: center;
  justify-content: start;
}
.flex-right {
  display: flex;
  align-items: center;
  justify-content: end;
}
.sendMessageInput {
  width: 100%;
  background-color: #ffffff;
  display: flex;
  flex-direction: column;
  .sendTops {
    display: flex;
    align-items: center;
    width: 100%;
    flex-shrink: 0;
  }
  .sendInput {
    display: flex;
    align-items: center;
    width: 100%;
    height: auto;
    border-radius: 50px;
    margin: 10px 10px;
    font-size: 26px;
    flex: 1;
    background-color: #f2f2fb;
    input {
      // flex: 1;
      width: 95%;
      background-color: #f2f2fb;
      height: 40px;
      border-radius: 25px;
      padding: 0 10px;
      font-size: 20px;
      outline: none;
      margin-left: 5px;
      border: none;
    }
    img {
      margin-right: 20px;
      width: 50px;
      height: 50px;
    }
  }
  img {
    width: 60px;
    height: 60px;
  }
}
.sendContent {
  width: 100%;
  background-color: #fff;
  border-top: 1px solid #eee;
  margin-top: 1px;
  // flex: 1;
  .emoji {
    width: 95%;
    display: flex;
    flex-wrap: wrap;
    overflow-x: scroll;
    font-size: 22px;
    line-height: 70px;
    margin: 10px 5px;
  }
}
.emojiImage {
  margin-right: 20px;
  background-image: url(../../assets/customer_img/smell.png);
  background-repeat: no-repeat;
  background-size: cover;
  width: 40px;
  height: 40px;
  display: inline-block;
}
.InputImage {
  margin-right: 20px;
  background-image: url(../../assets/customer_img/jian.png);
  background-repeat: no-repeat;
  background-size: cover;
  width: 40px;
  height: 40px;
  display: inline-block;
}
.sendFooter {
  height: 10px;
  width: 100%;
  background-color: #cfd1d3;
}
</style>

<script setup lang="ts">
import axios from 'axios'
import { onMounted, ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'

const route = useRoute()
const router = useRouter()
const fileInput = ref(null)
const MessageE = ref('')
const dialogVisible = ref(false)
const emojis = [
  '😀',
  '😄',
  '😁',
  '😆',
  '😅',
  '🤣',
  '😂',
  '🙂',
  '🙃',
  '😉',
  '😊',
  '😇',
  '😕',
  '😟',
  '🙁',
  '😮',
  '😯',
  '😰',
  '😓',
  '😩',
  '😫',
  '😠',
  '🤬',
  '😚',
  '😙',
  '🥲',
  '😋',
  '😛',
  '😜',
  '🤪',
  '😝',
  '👾',
  '🤖',
  '😺',
  '😸',
  '😹',
  '😻',
  '😼',
  '😽',
  '🥳',
  '🥸',
  '😎',
  '👁‍🗨',
  '💤',
  '💭',
  '🗯',
  '🗨',
  '👁️‍🗨️',
]
const showEmojiDown = ref(false)
const showEmoji = ref(true)

const DateTime = () => {
  const dateStr = new Date().toLocaleString()
  const parts = dateStr.split(' ')[1]
  const timeStr = parts.split(':').slice(0, 3).join(':')
  return timeStr
}
let ws = null

const backClick = () => {
  dialogVisible.value = true
}

const messageList = ref<any>([
  {
    type: 'server',
    content:
      '您好，我是圆心大药房客服，请问有什么可以帮您?为了方便您咨询，可以简单描述一下问题 我们尽快帮您处理',
    time: DateTime(),
  },
])
const handleImage = () => {
  showEmojiDown.value = !showEmojiDown.value
  showEmoji.value = !showEmoji.value
}
const formDate = new FormData()
console.log()

const handleFile = async (files: any) => {
  const file = files.target.files[0]
  console.log(file)
  formDate.append('file', file)
  await axios
    .post('/addres/upload', formDate)
    .then((res) => {
      console.log(res)
      messageList.value.push({
        type: 'person',
        content: res.data.result,
        time: DateTime(),
      })
      ws.send(JSON.stringify({ type: 'person', content: res.data.result, time: DateTime() }))
    })
    .catch((err) => {
      console.log(err)
      messageList.value.push({
        type: 'person',
        content: '图片上传失败，请稍后再试',
        time: DateTime(),
      })
    })
}

const handlePic = () => {
  fileInput.value.click()
}

const sendEmoji = (item: any) => {
  console.log(item)
  MessageE.value = MessageE.value.concat(item)
}

console.log(DateTime())

const handleSend = () => {
  console.log('发送消息')
  messageList.value.push({
    type: 'person',
    content: MessageE.value,
    time: DateTime(),
  })
  const jsonData = {
    type: 'person',
    content: MessageE.value,
    time: DateTime(),
  }
  ws.send(JSON.stringify(jsonData))
  console.log(JSON.stringify(jsonData))

  MessageE.value = ''
}

const initMessage = () => {
  ws = new WebSocket('ws://localhost:8090/')
  ws.onopen = function () {
    console.log('连接成功')
  }

  ws.onmessage = function (event) {
    const data = JSON.parse(event.data)
    console.log('收到数据', data)
    setTimeout(() => {
      messageList.value.push({
        type: 'server',
        content: data.content,
        time: DateTime(),
      })
    }, 1200)
  }
}

watch(messageList.value, (newValue, oldValue) => {
  console.log(newValue, oldValue)
  sessionStorage.setItem('messageList', JSON.stringify(newValue))
})

onMounted(() => {
  initMessage()
})
</script>
